// Day9手写： 
// 1.算法题：https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {ListNode} head
 * @return {TreeNode}
 */
 var sortedListToBST = function(head) {
    let arr=[]
    while(head){
        arr.push(head.val);
        head=head.next
    }
    const build = (arr)=>{
        if(arr.length==0) return null;
        let mid=Math.floor(arr.length/2);
        
        const root = new TreeNode(arr[mid]);
        root.left = build(arr.slice(0,mid))
        root.right = build(arr.slice(mid+1))
        return root;
    }
    return build(arr)
};


// 2.手写题：https://bigfrontend.dev/zh/problem/create-an-Event-Emitter
class EventEmitter {
    constructor() {
      //维护一个订阅该对象的map
      //map:  (eventName,[callback...]),key为eventName，value是由相同eventName的回调函数组成的数组
      //key: eventName,value: An array of callback functions with the same eventName
      this.watcher = new Map();
    }
    subscribe(eventName, callback) {
      var watcher=this.watcher;  
      if (!watcher.has(eventName)) {
        //eventName不存在，则添加
        //eventName does not exist,add
        watcher.set(eventName, [callback]);
      } else {
        //存在则向对应value里增加callback
        //eventName exists,add callback to the corresponding value
        watcher.set(eventName, [...watcher.get(eventName),callback]);
      }
      return {
        //返回一个包含release方法的对象
        //return an object containing the release method
        release: function () {
          //找到对应的callback，删除
          //find the corresponding callback and delete it
          //此时利用闭包，使用的是第10行的watcher
          //At this point,the Closures is used,and the watcher on line 10 is used
          watcher.get(eventName).map((item,index) => {
            if (item == callback) {
              watcher.get(eventName).splice(index,1);
            } else {
              return item;
            }
          })
        }
      }
    }
  
    emit(eventName, ...args) {
      if (this.watcher.has(eventName)) {
        //eventName存在则依此调用watcher里的callback
        //if eventName exists,call the callback in watcher accordingly
        this.watcher.get(eventName).forEach(call => {
          call.apply(this, args);
        })
      }
    }
  }
  
  //Closures:  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures